package arbor.mining.util.net;
import java.io.*;
import java.util.Properties;
import java.util.Date;

import javax.mail.*;
import javax.mail.internet.*;

import arbor.mining.util.UtilItem;


/**
 * EmailTask is used to send email. Please change the default Email
 * properties as your emailer. 
 */
public class EmailTask extends UtilItem {
	public static void SendHtmlMail(String[] argv) {
	}

	@Override
	public Properties getInitProperty() {
		Properties p = new Properties();
    p.put("EMAIL_SERVER", "arbor.ee.ntu.edu.tw");
    p.put("EMAIL_USER", "doug");
    p.put("EMAIL_PW", "doug");
    return p;
	}
  
	public static void main(String[] args) {
		EmailTask emailer = new EmailTask();
		/* use sendSimpleMail to send a plain text mail. */		
		emailer.sendSimpleMail("doug@arbor.ee.ntu.edu.tw", "doug@arbor.ee.ntu.edu.tw",
        "test", "mail text");
		
		/* attached file is permitted in sendMixedMails. */
		emailer.sendMixedMail("doug@arbor.ee.ntu.edu.tw", "doug@arbor.ee.ntu.edu.tw",
				"File submission", "xx file", new File("daily/stock.log"));				
	}

	/* send text message */
	public void sendSimpleMail(String to, String from, String subject, String text) {
		String cc = null, bcc = null;
    String mailer = "msgsend";
		String file = null;
		boolean debug = false;

		try {
			Properties props = new Properties();
			// XXX - could use Session.getTransport() and Transport.connect()
			// XXX - assume we're using SMTP			
			props.put("mail.host", prop.getProperty("EMAIL_SERVER"));
			props.put("mail.transport.protocol", "smtp");
			props.put("mail.send.user", prop.getProperty("EMAIL_USER"));
			props.put("mail.send.password", prop.getProperty("EMAIL_PW"));

			// Get a Session object
			Session session = Session.getInstance(props, null);
			if (debug)
				session.setDebug(true);

			// construct the message
			Message msg = new MimeMessage(session);
			if (from != null)
				msg.setFrom(new InternetAddress(from));
			else
				msg.setFrom();

			msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
					to, false));
			if (cc != null)
				msg.setRecipients(Message.RecipientType.CC, InternetAddress
						.parse(cc, false));
			if (bcc != null)
				msg.setRecipients(Message.RecipientType.BCC, InternetAddress
						.parse(bcc, false));

			msg.setSubject(subject);

			if (file != null) {
				// Attach the specified file.
				// We need a multipart message to hold the attachment.
				MimeBodyPart mbp1 = new MimeBodyPart();
				mbp1.setText(text);
				MimeBodyPart mbp2 = new MimeBodyPart();
				mbp2.attachFile(file);
				MimeMultipart mp = new MimeMultipart();
				mp.addBodyPart(mbp1);
				mp.addBodyPart(mbp2);
				msg.setContent(mp);
			} else {
				// If the desired charset is known, you can use
				// setText(text, charset)
				msg.setText(text);
			}

			msg.setHeader("X-Mailer", mailer);
			msg.setSentDate(new Date());

			// send the thing off
			Transport.send(msg);

			logger.info("Send Email Success: "+msg);
		} catch (Exception e) {
			logger.error(e);
		}
	}

	/**
	 * sendMixedMail creates a multipart/mixed message and sends it.
	 */
	public void sendMixedMail(String to, String from, String subject, String text, File file) {
		String msgText2 = text;
		boolean debug = false;

		// create some properties and get the default Session
		Properties props = new Properties();
		props.put("mail.host", prop.getProperty("EMAIL_SERVER"));
		props.put("mail.transport.protocol", "smtp");
		props.put("mail.send.user", prop.getProperty("EMAIL_USER"));
		if (prop.getProperty("EMAIL_PW") != null) {
		  props.put("mail.send.password", prop.getProperty("EMAIL_PW"));
    }

		Session session = Session.getInstance(props, null);
		session.setDebug(debug);

		try {
			// create a message
			MimeMessage msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress(from));
			InternetAddress[] address = { new InternetAddress(to) };
			msg.setRecipients(Message.RecipientType.TO, address);
			msg.setSubject(subject);
			msg.setSentDate(new Date());

			// create and fill the first message part
			MimeBodyPart mbp1 = new MimeBodyPart();
			mbp1.attachFile(file);

			// create and fill the second message part
			MimeBodyPart mbp2 = new MimeBodyPart();
			// Use setText(text, charset), to show it off !
			mbp2.setText(msgText2, "us-ascii");

			// create the Multipart and its parts to it
			Multipart mp = new MimeMultipart();
			mp.addBodyPart(mbp1);
			mp.addBodyPart(mbp2);

			// add the Multipart to the message
			msg.setContent(mp);
			// send the message
			Transport.send(msg);
			logger.info("Send Email Success: " + msg);
		} catch (MessagingException mex) {
			logger.error(mex);
		} catch (IOException e) {
			logger.error(e);
		}
	}
}
